import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:frametrim/app/app_navigator.dart';
import 'package:frametrim/appviews/governmentIndexView.dart';
import 'package:frametrim/http/ApiUtils.dart';
import 'package:frametrim/http/HttpManager.dart';
import 'package:frametrim/model/MyBlockData.dart';
import 'package:frametrim/model/MyBlocksM.dart';
import 'package:frametrim/route/Routes.dart';
import 'package:frametrim/utility/screen.dart';
import 'package:frametrim/utility/sq_color.dart';
import 'package:frametrim/views/background_view.dart';
import 'package:frametrim/views/badgeView.dart';
import 'package:frametrim/views/lineargradient_view.dart';
import 'package:get/get.dart';

import '../../appviews/refresh/CustomRefreshFooter.dart';
import '../../appviews/refresh/CustomRefreshHeader.dart';

/*政府端
* 服务首页
* 问题2：消息小红点儿，使用Badges时候，num位数越大，小红点越大，使用自定义时候，个位数时候不圆润
* */
class GovernmentServicePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => GovernmentServiceState();
}

class GovernmentServiceState extends State<GovernmentServicePage> {
  //广告图
  List<Image> images = [
    Image.asset(
      'image/banner.png',
      fit: BoxFit.fill,
    ),
  ];

//我的待办
  var mTitle = ["项目", "账号", "问题"];
  var mIntro = ["项目信息审核", "账号信息审核", "问题信息审核"];
  var mBackground = [Color(0xffDBEAFF), Color(0xffFCF3E3), Color(0xffFDE7E7)];
  var mPic = [
    "image/icon_index_project.png",
    "image/icon_index_account.png",
    "image/icon_index_problem.png"
  ];
  var mRedNum = [08, 16, 210];

  var mPage = 1;

  //滚动位置监听
  late ScrollController _scrollController;
  late double _scrollPosition;
  late EasyRefreshController _refreshController;

  _scrollListener() {
    /* setState(() {
      _scrollPosition = _scrollController.position.pixels;
    });*/
  }

  //我的待办
  var projectTotal = 0;
  var enterpriseTotal = 0;
  var problemTotal = 0;
  List<MyBlockData> myBlockData = [];

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _scrollController.addListener(_scrollListener);
    _refreshController = EasyRefreshController();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: SQColor.color_background,
      body: Stack(children: [
        //渐变背景
        GradientView(
          colors: [Color(0XFFA4C2FF), Color(0XFFF4F5F9)],
          mHeigth: 250,
        ),
        /*EasyRefresh.custom(
          controller: _refreshController,
          //上面创建的刷新控制器
          header: CustomRefreshHeader(),
          footer: CustomRefreshFooter(),
          onRefresh: () async {
            mPage = 1;
            getData();
          },
          onLoad: () async {
            mPage++;
            getData();
          },
          slivers: [*/

        ListView(
          padding: EdgeInsets.only(top: 10),
          shrinkWrap: true,
          controller: _scrollController,
          children: [
            //欢迎语
            buildWelcome(),
            //广告图
            //  BannerView(images,(index){}),
            buildImage(),
            buildMyBackLog(),
            buildWorkBench(),
            SizedBox(height: 10)
          ],
        )
        //   ],
        // ),
      ]),
    );
  }

  /*欢迎语*/
  Widget buildWelcome() {
    return Container(
      margin: EdgeInsets.only(left: 12, top: 10 + Screen.topSafeHeight),
      child: Text(
        "欢迎来到郑州建设劳务服务平台",
        style: TextStyle(
            fontSize: 15,
            color: SQColor.color_90black,
            fontWeight: FontWeight.bold),
      ),
    );
  }

  /*广告图*/
  Widget buildImage() {
    return Container(
      margin: EdgeInsets.only(left: 10, right: 10, top: 10),
      width: Screen.width - 20,
      height: (Screen.width - 20) / 18 * 10,
      child: Image.asset(
        "image/banner.png",
        fit: BoxFit.fill,
      ),
    );
  }

  /*我的待办*/
  Widget buildMyBackLog() {
    return BackgroundView(
      widget: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          buildModuleTitle('image/icon_index_backlog.png', '我的待办'),
          SizedBox(
            height: 10,
          ),
          Wrap(
            runAlignment: WrapAlignment.start,
            //水平间距
            spacing: 10,
            //垂直间距
            runSpacing: 10,
            //对齐方式
            alignment: WrapAlignment.start,
            children: getBlockData(),
          ),
          SizedBox(
            height: 5,
          )
        ],
      ),
    );
  }

  /*模块儿标题*/
  Widget buildModuleTitle(String img, String title) {
    return Row(
      // crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Image.asset(
          img,
          width: 20,
          height: 20,
        ),
        SizedBox(
          width: 5,
        ),
        Text(
          title,
          style: TextStyle(
              fontSize: 16,
              color: SQColor.color_90black,
              fontWeight: FontWeight.bold),
        )
      ],
    );
  }

  /*待办数据*/
  List<Widget> getBlockData() {
    List<Widget> children = [];
    if (myBlockData.length > 0) {
      for (var i = 0; i < myBlockData.length; i++) {
        children.add(Container(
            margin: EdgeInsets.only(top: 10),
            padding: EdgeInsets.all(0),
            width: (Screen.width - 60) / 2,
            child: BradgeView(
              mRedNum: myBlockData[i].total!,
              child: BackgroundView(
                mColor: myBlockData[i].background!,
                mVerticalTopMargin: 0,
                mVerticalBottomMargin: 0,
                mHorizontalMargin: 0,
                mHorizontalPadding: 10,
                mVerticalPadding: 15,
                widget: Row(
                  children: [
                    Image.asset(
                      myBlockData[i].img!,
                      width: 42,
                      height: 42,
                      fit: BoxFit.fill,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          myBlockData[i].title!,
                          style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: SQColor.color_3D3D3D),
                        ),
                        Text(
                          myBlockData[i].intro!,
                          style: TextStyle(
                              fontSize: 12, color: SQColor.color_3D3D3D),
                        )
                      ],
                    )
                  ],
                ),
              ),
            )));
      }
    } else {
      children.add(Container(
          width: Screen.width - 20,
          height: 100,
          child: Center(
            child: Text('暂无待办信息',
                style: TextStyle(fontSize: 16, color: SQColor.color_888888)),
          )));
    }
    return children;
  }

/*工作台*/
  Widget buildWorkBench() {
    return BackgroundView(
      mVerticalTopMargin: 0,
      mVerticalBottomMargin: 0,
      widget: Column(
        children: [
          buildModuleTitle('image/icon_work.png', '工作台'),
          SizedBox(height: 10),
          buildWorkBeachProject(),
          Divider(height: 0.5, color: SQColor.color_E5E5E5),
          SizedBox(height: 10),
          buildWorkBeachCompany(),
          Divider(height: 0.5, color: SQColor.color_E5E5E5),
          SizedBox(height: 10),
          buildWorkBeachLiangHua()
        ],
      ),
    );
  }

  /*工作台 - 项目信息*/
  Widget buildWorkBeachProject() {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('项目信息',
              style: TextStyle(fontSize: 15, color: SQColor.color_90black)),
          SizedBox(height: 10),
          Row(
            children: [
              buildWorkBeachItem(
                  imgStr: "image/icon_projectmess_01.png",
                  name: "项目信息管理",
                  onPress: JumpToProjectMessManagerPage,
                  mAlignment: Alignment.centerLeft),
              buildWorkBeachItem(
                  imgStr: "image/icon_projectmess_02.png",
                  name: "项目考勤管理",
                  onPress: () {
                    Get.toNamed(Routes.PROJECT_ATTENDANCE_MANAGER_LIST);
                  }),
              buildWorkBeachItem(
                  imgStr: "image/icon_salary.png",
                  name: "项目工资发放",
                  onPress: () {
                    Get.toNamed(Routes.Project_Salary_Issue_List);
                  },
                  mAlignment: Alignment.centerRight),
            ],
          ),
          SizedBox(height: 20)
        ],
      ),
    );
  }

  /*工作台 - 企业信息*/
  Widget buildWorkBeachCompany() {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('企业信息',
              style: TextStyle(fontSize: 15, color: SQColor.color_90black)),
          SizedBox(height: 10),
          Row(
            children: [
              buildWorkBeachItem(
                  imgStr: "image/icon_company_01.png",
                  name: "企业账号管理",
                  onPress: () {
                    Get.toNamed(Routes.Company_Account_Manage_List);
                  },
                  mAlignment: Alignment.centerLeft),
              buildWorkBeachItem(
                  imgStr: "image/icon_company_02.png",
                  name: "企业信息管理",
                  onPress: () {
                    Get.toNamed(Routes.Company_Mess_Manage_List);
                  }),
              //不显示但是占位
              Visibility(
                child: buildWorkBeachItem(
                    imgStr: "image/icon_salary.png",
                    name: "项目工资发放",
                    onPress: () {}),
                visible: false,
                maintainState: true,
                maintainAnimation: true,
                maintainSize: true,
              )
            ],
          ),
          SizedBox(height: 20)
        ],
      ),
    );
  }

/*工作台 - 量化管理*/
  Widget buildWorkBeachLiangHua() {
    return Container(
      width: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('市场行为信用管理',
              style: TextStyle(fontSize: 15, color: SQColor.color_90black)),
          SizedBox(height: 10),
          Wrap(
            runSpacing: 15,
            children: [
              buildWorkBeachItem(
                  imgStr: "image/icon_projectmess_01.png",
                  name: "    问题上报    ",
                  onPress: () {},
                  mAlignment: Alignment.centerLeft),
              buildWorkBeachItem(
                  imgStr: "image/icon_projectmess_02.png",
                  name: "项目信用评分",
                  onPress: () {
                    Get.toNamed(Routes.Project_Credit_Point_List);
                  }),
              buildWorkBeachItem(
                  imgStr: "image/icon_salary.png",
                  name: "企业信用评分",
                  onPress: () {
                    Get.toNamed(Routes.Company_Credit_Point_List);
                  },
                  mAlignment: Alignment.centerRight),
              buildWorkBeachItem(
                  imgStr: "image/icon_salary.png",
                  name: "问题上报审核",
                  onPress: () {},
                  mAlignment: Alignment.centerLeft),
            ],
          ),
        ],
      ),
    );
  }

  void JumpToProjectMessManagerPage() {
    // AppNavigator.push(context, ProjectMessManagerPage());
    Get.toNamed(Routes.PROJECT_MESS_MANAGER_LIST);
  }

  getData() {
    getMyBlocks();
  }

  /**
   * 获取我的待办
   */
  void getMyBlocks() async {
    try {
      HttpManager.getInstance().addHeader().get(
          context, ApiUtils.appCheckDataCount,
          params: null,
          contentType: 'multipart/form-data', successCallBack: (dataMap) {
        MyBlocksM model = MyBlocksM.fromJson(dataMap);
        myBlockData.clear();
        setState(() {
          if (model.records?.projectTotal != 0) {
            myBlockData.add(MyBlockData(
                total: model.records?.projectTotal,
                img: "image/icon_index_project.png",
                title: "项目",
                intro: "项目信息审核",
                background: Color(0xFFDBEAFF)));
          }
          if (model.records?.enterpriseTotal != 0) {
            myBlockData.add(MyBlockData(
                total: model.records?.enterpriseTotal,
                img: "image/icon_index_account.png",
                title: "账号",
                intro: "账号信息审核",
                background: Color(0xFFFCF3E3)));
          }
          if (model.records?.problemTotal != 0) {
            myBlockData.add(MyBlockData(
                total: model.records?.enterpriseTotal,
                img: "image/icon_index_problem.png",
                title: "问题",
                intro: "问题信息审核",
                background: Color(0xFFFDE7E7)));
          }
        });
      }, afterCallBack: () {
        _refreshController.finishLoad();
        _refreshController.finishRefresh();
      });
    } catch (e) {
      print(e);
    }
  }
}
